/*
    WiiCade Wii Remote API v2.0 
    Copyright (C) 2007 Jerason Banes

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    It is understood that no reaonable library system exists for Flash
    programs. To that end, this library is considered to be in library
    form when distributed as a source code package for linking to 
    Flash and Actionscript programs. Similarly, static linking of the
    library is understood to be the preferred form of distributing
    the library in binary form.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

import mx.events.EventDispatcher;

import com.wiicade.*;

class com.wiicade.WiiRemote
{
    static public var BUTTON_UP:Number = 8;
    static public var BUTTON_DOWN:Number = 4;
    static public var BUTTON_RIGHT:Number = 2;
    static public var BUTTON_LEFT:Number = 1;
    static public var BUTTON_1:Number = 64;
    static public var BUTTON_2:Number = 32;
    static public var BUTTON_A:Number = 256;
    static public var BUTTON_B:Number = 128;
    static public var BUTTON_PLUS:Number = 16;
    static public var BUTTON_MINUS:Number = 512;

    private var identity:Number;
    private var primary:Boolean = false;
    private var connected:Boolean = false;

    private var rotated:Boolean = false;
    private var allowRotate:Boolean = true;
    private var twistAngle:Number = 0;
    private var distance:Number = 0;
    
    private var cursorx:Number = 0;
    private var cursory:Number = 0;
    private var pressed:Number = 0;
        
    public var addEventListener:Function;
    public var removeEventListener:Function;
    public var dispatchEvent:Function;
    
    private var oldcursorx;
    private var oldcursory;
    
    private var activated;
    private var attachment;

    public function WiiRemote(identity:Number)
    {
        this.identity = identity;
        this.allowRotate = Wii.isWii();
        this.attachment = new Nunchuk(this);
        this.activated = new Array();

        //Default primary
        if(this.identity == 0) 
        {
            this.primary = true;
            this.connected = true;
        }
        
        EventDispatcher.initialize(this);
    }

    static function init():Void
    {
    }

    public function getIdentity():Number
    {
        return this.identity;
    }

    public function isPrimary():Boolean
    {
        return this.primary;
    }

    public function isConnected():Boolean
    {
        return this.connected;
    }

    public function isRotated():Boolean
    {
        return this.rotated;
    }

    public function setRotated(rotate:Boolean):Void
    {
        this.rotated = rotate;
    }

    public function getTwistAngle():Number
    {
        if(!this.activated[Wii.COMMAND_TWIST] && Wii.isWii())
        {
            getURL("javascript:activateWiiRemoteFeature("+identity+", "+(Wii.COMMAND_TWIST+this.identity)+", true);");
            this.activated[Wii.COMMAND_TWIST] = true;
        }
        
        return this.twistAngle;
    }
    
    public function getDistance():Number
    {
        if(!this.activated[Wii.COMMAND_DISTANCE] && Wii.isWii())
        {
            getURL("javascript:activateWiiRemoteFeature("+identity+", "+(Wii.COMMAND_DISTANCE+this.identity)+", true);");
            this.activated[Wii.COMMAND_DISTANCE] = true;
        }
        
        return this.distance;
    }
    
    public function getCursorX():Number
    {
        if(!this.activated[Wii.COMMAND_CURSORX] && Wii.isWii())
        {
            getURL("javascript:activateWiiRemoteFeature("+identity+", "+(Wii.COMMAND_CURSORX+this.identity)+", true);");
            this.activated[Wii.COMMAND_CURSORX] = true;
        }
        
        return this.cursorx;
    }
    
    public function getCursorY():Number
    {
        if(!this.activated[Wii.COMMAND_CURSORX] && Wii.isWii())
        {
            getURL("javascript:activateWiiRemoteFeature("+identity+", "+(Wii.COMMAND_CURSORX+this.identity)+", true);");
            this.activated[Wii.COMMAND_CURSORX] = true;
        }
        
        return this.cursory;
    }
    
    public function isFacingScreen():Boolean
    {
        return (cursorx > 0 || cursory > 0);
    }
    
    public function isDown(buttonCode):Boolean
    {
        if(this.rotated && this.allowRotate)
        {
            if(buttonCode == BUTTON_UP) buttonCode = BUTTON_RIGHT;
            else if(buttonCode == BUTTON_RIGHT) buttonCode = BUTTON_DOWN;
            else if(buttonCode == BUTTON_DOWN) buttonCode = BUTTON_LEFT;
            else if(buttonCode == BUTTON_LEFT) buttonCode = BUTTON_UP;
        }

        return ((this.pressed & buttonCode) > 0);
    }
    
    public function getAttachment()
    {
        if(!this.activated[Wii.COMMAND_ATTACH] && Wii.isWii())
        {
            getURL("javascript:activateWiiRemoteFeature("+identity+", "+(Wii.COMMAND_ATTACH+this.identity)+", true);");
            this.activated[Wii.COMMAND_ATTACH] = true;
        }
        
        return this.attachment;
    }

    function buttonsUpdated(bitmap:Number):Void
    {
        var mask = 1;
        var oldpressed = this.pressed;
        var diff;

        this.pressed = bitmap;
        diff = this.pressed ^ oldpressed;

        for(var i=0; i<10; i++)
        {
             if((diff & mask) > 0)
             {
                 if((this.pressed & mask) > 0) dispatchEvent({type:"buttondown", buttonCode: mask, identity: this.identity});
                 else dispatchEvent({type:"buttonup", buttonCode: mask, identity: this.identity});
             }

             mask <<= 1;
        }
    }
    
    function twistUpdated(angle:Number):Void
    {
        var oldangle = this.twistAngle;
        
        this.twistAngle = angle;
        
        if(this.twistAngle != oldangle) dispatchEvent({type:"twist", angle: angle, identity: this.identity});
    }
    
    function distanceUpdated(distance:Number):Void
    {
        var olddistance = this.distance;
        
        this.distance = distance;
        
        if(this.distance != olddistance) dispatchEvent({type:"distance", distance: distance, identity: this.identity});
    }
    
    function connectedUpdated(connected:Boolean):Void
    {
        var oldconnected = this.connected;
        
        this.connected = connected;
        
        if(this.connected != oldconnected) 
        {
            if(connected) dispatchEvent({type:"connected", identity: this.identity});
            else dispatchEvent({type:"disconnected", identity: this.identity});
        }
    }
    
    function cursorXUpdated(cursorx:Number):Void
    {
        this.oldcursorx = this.cursorx;
        this.cursorx = cursorx;
    }
    
    function cursorYUpdated(cursory:Number):Void
    {
        this.oldcursory = this.cursory;
        this.cursory = cursory;
        
        if(this.cursorx != oldcursorx || this.cursory != oldcursory) 
        {
            dispatchEvent({type:"pointer", cursorx: this.cursorx, cursory: this.cursory, identity: this.identity});
        }
    }
    
    function updatePrimary(primary:Boolean)
    {
        this.primary = primary;
    }
    
    function updateAttachment(attachment)
    {
        this.attachment = attachment;
    }
}
